<template>
  <el-dialog v-loading="loading" :title="title" :visible.sync="dialogVisible" width="500px">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px" label-position="left">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name" type="name" placeholder="请输入内容" />
      </el-form-item>

      <el-form-item label="角色说明" prop="description">
        <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="hide">放弃</el-button>
      <el-button type="primary" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addRole, getRole, updateRole } from '@/api/role'

export default {
  props: {
    scope: {
      require: true,
      type: Number
    }
  },

  data() {
    return {
      resolve: null,
      reject: null,
      roleId: null,
      loading: false,
      dialogVisible: null,
      title: null,
      rules: {
        name: [{
          required: true,
          message: '请输入角色名称',
          trigger: 'blur'
        }],
        description: [{
          required: true,
          message: '请输入角色说明',
          trigger: 'blur'
        }]
      },
      form: {
        code: undefined,
        name: undefined,
        type: 0,
        description: undefined
      }
    }
  },

  methods: {
    defaultForm() {
      return {
        code: undefined,
        name: undefined,
        type: 0,
        description: undefined
      }
    },

    fetchData() {
      this.loading = true
      return getRole(this.roleId, this.scope).then((res) => {
        this.loading = false
        if (res.code === 0) {
          this.form = res.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch((error) => {
        console.error('getMenuInfo error', error)
        this.loading = false
      })
    },

    openAdd() {
      this.mode = 'add'
      this.title = '新增角色'
      this.roleId = null
      this.form = this.defaultForm()
      this.show()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    openEdit(roleId) {
      this.title = '编辑角色'
      this.mode = 'edit'
      this.roleId = roleId
      this.show()
      this.fetchData()
      return new Promise((resolve, reject) => {
        this.resolve = resolve
        this.reject = reject
      })
    },

    show() {
      this.dialogVisible = true
    },

    hide() {
      this.resolve = null
      this.reject = null
      this.dialogVisible = false
    },

    handleParentIdChange(value) {
      if (value && value.length > 0) {
        this.form.parentId = value[value.length - 1]
      } else {
        this.form.parentId = -1
      }
    },

    updateObject() {
      this.loading = true
      return updateRole(this.roleId, this.form, this.scope).then((res) => {
        this.loading = false
        if (res.code === 0) {
          this.$notify({ title: '成功', message: '更新成功', type: 'success', duration: 2000 })
          this.resolve && this.resolve()
          this.hide()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => {
        this.loading = false
      })
    },

    addObject() {
      if (!this.form.parentId) {
        this.form.parentId = -1
      }
      this.loading = true
      return addRole(this.form, this.scope).then((res) => {
        this.loading = false
        if (res.code === 0) {
          this.$notify({ title: '成功', message: '添加成功', type: 'success', duration: 2000 })
          this.resolve && this.resolve()
          this.hide()
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => {
        this.loading = false
      })
    },

    handleSubmit() {
      if (this.mode === 'edit') {
        this.updateObject()
      } else {
        this.addObject()
      }
    }
  }
}
</script>
